package com.xrit.controller;

import com.xrit.constant.MessageConstant;
import com.xrit.result.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("user")
public class UserController {

    @RequestMapping("/getUserName")
    public Result getUserName(){
        try{
            // 获取登陆用户的认证信息
            User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            // 登陆用户名
            String username = loginUser.getUsername();
            // 返回给前端
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }
}
